## Replication code for Estimating Candidates’ Political Orientation in a Polarized Congress
## We use R for 3.2.4 GUI 1.67 Mavericks build (7152), running on Mac OS 10.12.1 


load("ordfiles.RData")
library(oc)      ## We use version: oc_0.95
library(pscl)    ## We use version: pscl_1.4.9

#sen107 <- readKH("ftp://voteview.com/sen107kh.ord")
#sen108 <- readKH("ftp://voteview.com/sen108kh.ord")
#sen109 <- readKH("ftp://voteview.com/sen109kh.ord")
#sen110 <- readKH("ftp://voteview.com/sen110kh.ord")
#sen111 <- readKH("ftp://voteview.com/sen111kh.ord")
#sen112 <- readKH("ftp://voteview.com/sen112kh.ord")
#sen113 <- readKH("ftp://voteview.com/sen113kh.ord")

sen107oc <-oc(sen107,dims=1,polarity=c(2))
sen108oc <-oc(sen108,dims=1,polarity=c(2))
sen109oc <-oc(sen109,dims=1,polarity=c(2))
sen110oc <-oc(sen110,dims=1,polarity=c(2))
sen111oc <-oc(sen111,dims=1,polarity=c(2))
sen112oc <-oc(sen112,dims=1,polarity=c(2))
sen113oc <-oc(sen113,dims=1,polarity=c(2))

sen107oc2<-cbind(sen107oc$legislators$icpsrLegis ,sen107oc$legislators$coord1D)
sen107oc2<-as.data.frame(sen107oc2)
colnames(sen107oc2)<-c("ICPSR2", "OC")
sen107oc2$cong<-107

sen108oc2<-cbind(sen108oc$legislators$icpsrLegis ,sen108oc$legislators$coord1D)
sen108oc2<-as.data.frame(sen108oc2)
colnames(sen108oc2)<-c("ICPSR2", "OC")
sen108oc2$cong<-108

sen109oc2<-cbind(sen109oc$legislators$icpsrLegis ,sen109oc$legislators$coord1D)
sen109oc2<-as.data.frame(sen109oc2)
colnames(sen109oc2)<-c("ICPSR2", "OC")
sen109oc2$cong<-109

sen110oc2<-cbind(sen110oc$legislators$icpsrLegis ,sen110oc$legislators$coord1D)
sen110oc2<-as.data.frame(sen110oc2)
colnames(sen110oc2)<-c("ICPSR2", "OC")
sen110oc2$cong<-110

sen111oc2<-cbind(sen111oc$legislators$icpsrLegis ,sen111oc$legislators$coord1D)
sen111oc2<-as.data.frame(sen111oc2)
colnames(sen111oc2)<-c("ICPSR2", "OC")
sen111oc2$cong<-111

sen112oc2<-cbind(sen112oc$legislators$icpsrLegis ,sen112oc$legislators$coord1D)
sen112oc2<-as.data.frame(sen112oc2)
colnames(sen112oc2)<-c("ICPSR2", "OC")
sen112oc2$cong<-112

sen113oc2<-cbind(sen113oc$legislators$icpsrLegis ,sen113oc$legislators$coord1D)
sen113oc2<-as.data.frame(sen113oc2)
colnames(sen113oc2)<-c("ICPSR2", "OC")
sen113oc2$cong<-113

senate_oc_results<-rbind(sen107oc2, sen108oc2,sen109oc2,sen110oc2,sen111oc2,sen112oc2,sen113oc2)

#house107 <- readKH("ftp://voteview.com/HOU107kh.ord")
#house108 <- readKH("ftp://voteview.com/HOU108kh.ord")
#house109 <- readKH("ftp://voteview.com/HOU109kh.ord")
#house110 <- readKH("ftp://voteview.com/HOU110kh.ord")
#house111 <- readKH("ftp://voteview.com/HOU111kh.ord")
#house112 <- readKH("ftp://voteview.com/HOU112kh.ord")
#house113 <- readKH("ftp://voteview.com/HOU113kh.ord")

house107oc <-oc(house107,dims=1,polarity=c(2))
house108oc <-oc(house108,dims=1,polarity=c(2))
house109oc <-oc(house109,dims=1,polarity=c(2))
house110oc <-oc(house110,dims=1,polarity=c(2))
house111oc <-oc(house111,dims=1,polarity=c(2))
house112oc <-oc(house112,dims=1,polarity=c(2))
house113oc <-oc(house113,dims=1,polarity=c(2))

house107oc2<-cbind(house107oc$legislators$icpsrLegis ,house107oc$legislators$coord1D)
house107oc2<-as.data.frame(house107oc2)
colnames(house107oc2)<-c("ICPSR2", "OC")
house107oc2$cong<-107

house108oc2<-cbind(house108oc$legislators$icpsrLegis ,house108oc$legislators$coord1D)
house108oc2<-as.data.frame(house108oc2)
colnames(house108oc2)<-c("ICPSR2", "OC")
house108oc2$cong<-108

house109oc2<-cbind(house109oc$legislators$icpsrLegis ,house109oc$legislators$coord1D)
house109oc2<-as.data.frame(house109oc2)
colnames(house109oc2)<-c("ICPSR2", "OC")
house109oc2$cong<-109

house110oc2<-cbind(house110oc$legislators$icpsrLegis ,house110oc$legislators$coord1D)
house110oc2<-as.data.frame(house110oc2)
colnames(house110oc2)<-c("ICPSR2", "OC")
house110oc2$cong<-110

house111oc2<-cbind(house111oc$legislators$icpsrLegis ,house111oc$legislators$coord1D)
house111oc2<-as.data.frame(house111oc2)
colnames(house111oc2)<-c("ICPSR2", "OC")
house111oc2$cong<-111

house112oc2<-cbind(house112oc$legislators$icpsrLegis ,house112oc$legislators$coord1D)
house112oc2<-as.data.frame(house112oc2)
colnames(house112oc2)<-c("ICPSR2", "OC")
house112oc2$cong<-112

house113oc2<-cbind(house113oc$legislators$icpsrLegis ,house113oc$legislators$coord1D)
house113oc2<-as.data.frame(house113oc2)
colnames(house113oc2)<-c("ICPSR2", "OC")
house113oc2$cong<-113

house_oc_results<-rbind(house107oc2, house108oc2,house109oc2,house110oc2,house111oc2,house112oc2,house113oc2)

save(house_oc_results,senate_oc_results, file="OptimalClassification_results.RData")
